package com.miracle.main.module.mvp.ui.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.kyleduo.switchbutton.SwitchButton;
import com.miracle.basemvp.base.BaseActivity;
import com.miracle.basemvp.di.component.AppComponent;
import com.miracle.common.adapter.SingleSelectAdapter;
import com.miracle.common.bean.User.CheckBean;
import com.miracle.common.util.CommonConstants;
import com.miracle.common.util.RadioButtonUtil;
import com.miracle.common.util.S;
import com.miracle.common.util.Utils;
import com.miracle.common.view.BaseDialogProvider;
import com.miracle.common.view.NumEditText;
import com.miracle.main.R;
import com.miracle.main.R2;
import com.miracle.main.module.di.component.DaggerAddSecretComponent;
import com.miracle.main.module.di.module.AddSecretModule;
import com.miracle.main.module.mvp.contract.AddSecretContract;
import com.miracle.main.module.mvp.entity.request.AddSecretRequest;
import com.miracle.main.module.mvp.presenter.AddSecretPresenter;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * com.miracle.main.module.mvp.ui.activity
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/7 10:29
 * @see com.miracle.main.module.mvp.ui.activity
 */
public class AddSecretActivity extends BaseActivity<AddSecretPresenter> implements
        AddSecretContract.View {

    @BindView(R2.id.tv_title)
    TextView mTvTitle;
    @BindView(R2.id.net)
    NumEditText mNumEditText;
    @BindView(R2.id.sw)
    SwitchButton mSw;
    @BindView(R2.id.rg)
    RadioGroup mRg;

//    @Inject
//    List<CheckBean> mList;
//    @Inject
//    SingleSelectAdapter mAdapter;

    AlertDialog mAddPicMethodDialog;
    BaseDialogProvider dialogProvider;

    LifecycleProvider<ActivityEvent> lifecycleProvider;


    @Override
    public int setLayoutId(@Nullable Bundle bundle) {
        return R.layout.cloud_activity_add_secret;
    }

    @Override
    public void initView() {
        mTvTitle.setText("创建秘密");
        initRadioGroup();
    }

    @Override
    public void initData() {

    }
    ArrayList<RadioButtonUtil.RadioBean> mRgList = new ArrayList<>();
    ArrayList<RadioButton> mRadioGroupList;
    /**
     * 创建性别radiogroup
     */
    private void initRadioGroup() {
        for (int i = 0; i < CommonConstants.Mood.VALUES.length; i++) {
            RadioButtonUtil.RadioBean bean = new RadioButtonUtil.RadioBean();
            bean.setId(CommonConstants.Mood.IDS[i]);
            bean.setText(CommonConstants.Mood.VALUES[i]);
            mRgList.add(bean);
        }

        mRadioGroupList = RadioButtonUtil.addButtonToRadioGroup(this, mRgList, mRg);
        RadioButton rbSex = (RadioButton) mRg.getChildAt(0);
        rbSex.setChecked(true);
    }

    @Override
    public void setDaggerComponent(AppComponent appComponent) {
        DaggerAddSecretComponent
                .builder()
                .appComponent(appComponent)
                .addSecretModule(new AddSecretModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onRequest(LifecycleProvider<ActivityEvent> lifecycleProvider) {
        this.lifecycleProvider = lifecycleProvider;
    }

    @OnClick({R2.id.iv_back, R2.id.tv_submit})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {

        }

        if (id == R.id.tv_submit) {
            submitData();
        }
    }

    /**
     * 提交数据
     */
    private void submitData() {
        String content = mNumEditText.getEditText().trim();
        String[] strs = {content};
        String[] errors = {"内容"};
        if (Utils.checkNullList(mTvTitle, strs, errors)) {
            return;
        }
        mSw.isChecked();
        AddSecretRequest request = new AddSecretRequest();
        request.setSaysth(content);
        request.setIsfriendfind((mSw.isChecked()) ? "1" : "0");
        request.setMood((RadioButtonUtil.getTagByRadioGroup(mRadioGroupList, mRg
                .getCheckedRadioButtonId()) + ""));
        mPresenter.addSecret(lifecycleProvider, request);
    }

    @Override
    public void showData(Object o) {

    }

    @Override
    public void addSucc() {
        S.Short(mTvTitle, "发布成功").info().show();
        finish();
    }

    @Override
    public void addFail(String msg) {

    }
}
